<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIButton
 *
 * @author I062261
 */
class BUIButton {

    protected $view = null;
    protected $name = null;
    protected $buttonStyle = 'bootstrap';
    protected $data = array();
    protected $buttonId = null;

    public function __construct($name, $data = NULL) {

        $this->name = $name;
        $this->buttonId = uniqid($this->name);

        //$this->view = $this->buttonStyle . '-button';

        if (is_string($data))
            $this->view = $data;

        if (is_array($data)) {
            if (isset($data[0]) && is_string($data[0])) {
                $this->setView($data[0]);
                if (isset($data[1]))
                    $this->setData($data[1]);
            }
        }

        if (isset($data['label']))
            $this->data['button'] = $data;
    }

    public function getName() {
        return $this->name;
    }

    public function getView($view = NULL) {

        if ($view != NULL)
            $this->view = $view;

        if ($this->view == NULL)
            $this->view = $this->buttonStyle . "-button";

        return $this->view;
    }

    public function setButtonStyle($style) {
        $this->buttonStyle = $style;
    }

    public function setView($view) {
        $this->view = $view;
    }

    public function getData($data = NULL) {

        $currentScreen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();

        $buttonId = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen()->getId()."_".  $this->getName();

        if (is_array($data))
            $this->data = array_merge($this->data, $data);

        $button = $this->data['button'];

        $button['url'] = isset($button['url']) ? $button['url'] : $currentScreen->getUrl();

        if (isset($button['url'])) {
            $url = $button['url'];
            $url[$buttonId] = '';
            $button['url'] = $url;
            $this->data['button'] = $button;
        }
        return $this->data;
    }

    public function setData($data) {
        $this->data = $data;
    }

    public function isClicked() {
        //$currentScreen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
        $buttonId = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen()->getId()."_".  $this->getName();
        if (isset($_GET[$buttonId])) {
            return TRUE;
        }

        return FALSE;
    }

    public function echoButton() {

        $currentScreen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
        $buttonId = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen()->getId() ."_". $this->getName();

        $this->getData();

        $url = $currentScreen->getUrl(array($buttonId => ''));

        if (isset($this->data['label']))
            $button = $this->data;

        if (isset($this->data['button']))
            $button = $this->data['button'];

        $type = isset($button['type']) ? $button['type'] : '';
        $buttonType = isset($button['buttonType']) ? $button['buttonType'] : 'link';
        $htmlOptions = isset($button['htmlOptions']) ? $button['htmlOptions'] : '';

        //if($button['buttonType'] == "reset")
        //$url = "";

        $controller = yii::app()->getController();
        $controller->widget('bootstrap.widgets.TbButton', array(
            'label' => $button['label'],
            'buttonType' => $buttonType,
            'type' => $type,
            'url' => $url,
            'htmlOptions' => $htmlOptions
                )
        );
    }

}

?>
